<!---- Creator: Eoly -->
<!---- On 2017/1/10 -->
<!---- Call http://palerock.cn -->

<!--
    获取一个订单详细信息API（包含订单绑定的团和地址信息）：
    method:GET
    url:'http://palerock.cn/user/order/find-order-deeply'

    请求头:
    Authorization:'Bearer '+token 密文记录

    params:
    {                           // 发送params参数
        无
    }
    返回：
    {
        dataList:       // 返回的团数据列表
            {
                orderId:                  // 订单id
                orderUserId:              // 商家id
                orderMoney:               // 订单金额
                orderAddressId:           // 订单地址id
                orderAddTime:             // 订单生成时间
                orderStatus:              // 订单状态
                                                -1.失效
                                                0.未付款
                                                1.付款成功，等待拼团
                                                2.拼团成功，等待发货
                                                3.交易成功
            }
            ...


        error:      // '0'则为操作成功

        mes:        // 操作后返回的信息
    }
-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>AngularTest</title>
    <style>
        * {
            /*padding: 0;*/
            margin: 0;
            /*border: 0;*/
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }
    </style>
    <script src="../angular.js"></script>
    <script>

       var net = 'http://palerock.cn/h5_back';

        var app = angular.module('app', []);

       //自定义过滤器——转换订单状态为文字
       app.filter('explainOrderStatus', function () {
           return function (status) {
               var result = '';
               switch (status) {
                   case -1:
                       result = '已失效';
                       break;
                   case 0:
                       result = '未付款';
                       break;
                   case 1:
                       result = '付款成功，等待拼团';
                       break;
                   case 2:
                       result = '拼团成功，等待发货';
                       break;
                   case 3:
                       result = '交易成功';
                       break;
                   default:
                       result = '未知状态';
               }
               //input 就是我们要处理的数据
               return result;
           }
       });


        app.controller('info', function ($scope, $http) {


            //声明从缓存获取的团对象
            $scope.preOrder = null;

            //声明从服务器获取的订单列表
            $scope.orderList = [];


            //获取订单列表
            $scope.getOrderList = function(){

                var token = localStorage.getItem("token");

                if (!token) {
                    alert("您还没有登录");
                    location.href = "登陆简单演示.html";
                }


                $http({
                    method: 'get',
                    url: net + "/user/order/show-user-orders",
                    headers : {'Authorization' : 'Bearer ' + token}
                }).then(
                        function (res) {

                            if(!res.data.error) {

                                alert(res.data.mes);
                                console.log(res);
                                $scope.orderList = res.data.dataList;

                            }else{
                                alert("错误代码:"+res.data.error+","+res.data.mes)
                            }

                        },
                        //获取失败
                        function (res) {
                            alert('网络或设置错误');
                        }
                )
            };


            //跳转到支付页面
            $scope.pay = function(order){
                sessionStorage.setItem('pre_order',JSON.stringify(order));
                location.href = '订单支付页面演示.html';
            };

            //跳转到详情页面
            $scope.detail = function(order){
                sessionStorage.setItem('pre_order',JSON.stringify(order));
                location.href = '订单详情页面演示.html';
            };


            $scope.logout = function () {
                localStorage.removeItem("token");
                alert("您已注销成功");
                location.reload();
            };
        });
    </script>
</head>
<body ng-app="app">
<div ng-controller="info" ng-init="getOrderList();">
    <table>
        <tr ng-repeat="order in orderList">
            <td>
                <fieldset>
                    <legend align="center">{{order.orderAddTime | date:'yyyy-MM-dd HH:mm:ss'}}</legend>
                    订单id：{{order.orderId}}
                    <br/>
                    订单金额：{{order.orderMoney}}
                    <br/>
                    订单绑定地址信息id：{{order.orderAddressId}}
                    <br/>
                    订单绑定团id：{{order.groupId}}
                    <br/>
                    订单绑定团商家id：{{order.orderUserId}}
                    <br/>
                    订单状态：{{order.orderStatus | explainOrderStatus}}
                    <br/>
                    <button ng-show="order.orderStatus==0" ng-click="pay(order)">去支付</button>
                    <button ng-show="order.orderStatus!=0" ng-click="detail(order)">查看详情</button>
                </fieldset>
            </td>
        </tr>
    </table>
</div>

</body>
</html>